﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TPAFSVA06
    {
        VICC_EXART VEXART;
        public VHELP VHP
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VHP"))
                    ENVMAIN["VHP"] = new VHELP();
                return (VHELP)ENVMAIN["VHP"];
            }
        }

        #region ==IsPrint===========
        public static readonly DependencyProperty IsPrintProperty = DependencyProperty.Register("IsPrint",
            typeof(bool), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPrintChanged));
        public bool IsPrint
        {
            get
            {
                return (bool)GetValue(IsPrintProperty);
            }
            set
            {
                SetValue(IsPrintProperty, value);
                OnPrintChg();
            }
        }
        private static void OnIsPrintChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.IsPrint = (bool)e.NewValue;
            }
        }

        public void OnPrintChg()
        {
            if (TMV == null)
                return;
            TMV.IsPrint = IsPrint;
            if (IsPrint)
                return;
            TBDetail.Merge(TBMain);
            TBDetail.Clear();
        }
        #endregion

        #region ==REMARK===========
        public static readonly DependencyProperty REMARKProperty = DependencyProperty.Register("REMARK",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnREMARKChanged));
        public string REMARK
        {
            get
            {
                return (string)GetValue(REMARKProperty);
            }
            set
            {
                SetValue(REMARKProperty, value);

            }
        }
        private static void OnREMARKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.REMARK = (string)e.NewValue;
                src.OnRemarkChg((string)e.NewValue);
            }
        }
        public void OnRemarkChg(string remark)
        {
            if (dwMaster == null)
                return;
            dwMaster["REMARK"] = remark; 
            if (dwMain == null)
            { }
        }
        #endregion

        #region ==COMPOSITION===========
        public static readonly DependencyProperty COMPOSITIONProperty = DependencyProperty.Register("COMPOSITION",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCOMPOSITIONChanged));
        public string COMPOSITION
        {
            get
            {
                return (string)GetValue(COMPOSITIONProperty);
            }
            set
            {
                SetValue(COMPOSITIONProperty, value);

            }
        }
        private static void OnCOMPOSITIONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.COMPOSITION = (string)e.NewValue;
                src.OnCOMPOSITIONChg((string)e.NewValue);
            }
        }
        public void OnCOMPOSITIONChg(string composition)
        {
            if (dwMaster == null)
                return;
            dwMaster["COMPOSITION"] = composition;
            dwMaster["VAAFILLER"] = composition;
            if (dwMain == null)
            { }
        }
        #endregion


        
        #region ==WIDTH===========
        public static readonly DependencyProperty WIDTHProperty = DependencyProperty.Register("WIDTH",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWIDTHChanged));
        public string WIDTH
        {
            get
            {
                return (string)GetValue(WIDTHProperty);
            }
            set
            {
                SetValue(WIDTHProperty, value);
                OnWIDTHChg();
            }
        }
        private static void OnWIDTHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.WIDTH = (string)e.NewValue;
            }
        }
        public void OnWIDTHChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["WIDTH"] = WIDTH;
            if (dwMain == null)
            { }
        }
        #endregion
        
        #region ==CUSTOMERPRICE===========
        public static readonly DependencyProperty CUSTOMERPRICEProperty = DependencyProperty.Register("CUSTOMERPRICE",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCUSTOMERPRICEChanged));
        public string CUSTOMERPRICE
        {
            get
            {
                return (string)GetValue(CUSTOMERPRICEProperty);
            }
            set
            {
                SetValue(CUSTOMERPRICEProperty, value);
                OnCUSTOMERPRICEChg();
            }
        }
        private static void OnCUSTOMERPRICEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.CUSTOMERPRICE = (string)e.NewValue;
            }
        }
        public void OnCUSTOMERPRICEChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["VAAARTWEB"] = CUSTOMERPRICE;
            dwMaster["PRICE"] = CUSTOMERPRICE;
            if (dwMain == null)
            { }
        }
        #endregion
        
        #region ==WEIGHT===========
        public static readonly DependencyProperty WEIGHTProperty = DependencyProperty.Register("WEIGHT",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWEIGHTChanged));
        public string WEIGHT
        {
            get
            {
                return (string)GetValue(WEIGHTProperty);
            }
            set
            {
                SetValue(WEIGHTProperty, value);
                OnWEIGHTChg();
            }
        }
        private static void OnWEIGHTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.WEIGHT = (string)e.NewValue;
            }
        }
        public void OnWEIGHTChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["WEIGHT"] = WEIGHT;
            if (dwMain == null)
            { }
        }
        #endregion

        #region ==CODE===========
        public static readonly DependencyProperty CODEProperty = DependencyProperty.Register("CODE",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCODEChanged));
        public string CODE
        {
            get
            {
                return (string)GetValue(CODEProperty);
            }
            set
            {
                SetValue(CODEProperty, value);
                OnCODEChg();
            }
        }
        private static void OnCODEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.CODE = (string)e.NewValue;
            }
        }
        public void OnCODEChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["CODE"] = CODE;
            if (dwMain == null)
            { }
        }
        #endregion

        #region ==LOCATION===========
        public static readonly DependencyProperty LOCATIONProperty = DependencyProperty.Register("LOCATION",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLOCATIONChanged));
        public string LOCATION
        {
            get
            {
                return (string)GetValue(LOCATIONProperty);
            }
            set
            {
                SetValue(LOCATIONProperty, value);
                OnLOCATIONChg();
            }
        }
        private static void OnLOCATIONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.LOCATION = (string)e.NewValue;
            }
        }
        public void OnLOCATIONChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["LOCATION"] = LOCATION;
            if (dwMain == null)
            { }
        }
        #endregion
        
        #region ==BOOK===========
        public static readonly DependencyProperty BOOKProperty = DependencyProperty.Register("BOOK",
            typeof(string), typeof(TPAFSVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBOOKChanged));
        public string BOOK
        {
            get
            {
                return (string)GetValue(BOOKProperty);
            }
            set
            {
                SetValue(BOOKProperty, value);
                OnBOOKChg();
            }
        }
        private static void OnBOOKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPAFSVA06;
            if (src != null)
            {
                src.BOOK = (string)e.NewValue;
            }
        }
        public void OnBOOKChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["BOOK"] = BOOK;
            if (dwMain == null)
            { }
        }
        #endregion

        public TPAFSVA06()
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            this.DataContext = MV;
            InitializeComponent();
            this.cDR.Content = "Copies:";

        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            RESET();
            IsReset = true;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            cEit.IsEnabled = true;
            if (dwMain == null)
            {

            }
            try
            {
                dwVA = dwMaster;
                if (dwMaster == null)
                {
                    VAPR = "";
                    KEY = "";
                    PRART = "";
                    WSPRICE = "0";
                    CURRENCY = "";
                    PRMEEH = "";
                    TXTISIND = "";
                }
                else
                {

                    if (dwMaster["VAASPERRDAT"].ToString().Trim() != "0")
                    {
                        cEit.IsEnabled = false;
                    }
                    if (dwMaster["VAANEUDAT"].ToString().Trim() == "0")
                    {
                        dwMaster["VAANEUDAT"] = MV.date;
                    }
                    VAPR = "VA" + dwMaster["VAANEUDAT"].ToString().Substring(2, 6)
                        + "OF"
                        + CURRENCY + dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString();
                    KEY = dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString();
                    TXTISIND = (dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString()).PadRight(60);
                    PRART = dwMaster["VAAARTST"].ToString() + dwMaster["VAADART"].ToString() + dwMaster["VAADVAR"].ToString();
                    string PR = dwMaster["VAAARTWEB"].ToString().PadRight(15);
                    WSPRICE = PR.Substring(0, 8);
                    CURRENCY = PR.Substring(8, 3);
                    PRMEEH = PR.Substring(12, 3);
                    if (IsPrint)
                    {
                        dwData = TBDetail.Rows.Add();
                        dwData.ItemArray = dwMaster.ItemArray;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        public void UpdateByWS(MessageInfo msg)
        {
            if (dwMaster == null)
                return;
            if (cWS != null)
            {
                //dwMaster["WABIND"] = cWS.dwWS["WABIND"];
                dwMaster["LSLIEFNA"] = cWS.LSLIEFNA;
            }
            dwMaster["VAAZOLLTNR1"] = dwMaster["CODE"].ToString().PadRight(20).Substring(0,20);
            dwMaster["VAAREFART"] = dwMaster["LOCATION"].ToString().PadRight(24).Substring(0, 24);
            dwMaster["VAAARTFAM"] = dwMaster["BOOK"].ToString().PadRight(24).Substring(0, 24);
            dwMaster["VAAARTWEB"] = dwMaster["PRICE"].ToString().PadRight(24).Substring(0, 24);
            dwMaster["VAAAENDBEN"] = app.User;
            dwMaster["VAAAENDDAT"] = MV.date;
            TMV.UVAART06(dwMaster);
        }
        public double RESET()
        {
            prefix = TMV.KOLL.PadRight(5).Substring(0, 2);
            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAART"] = " AND VAAART LIKE '" + prefix + "%'";
            SResult = TMV.QVAMAX(dwData);
            double start = 0;
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                if (!row["VAAART"].ToString().StartsWith(prefix))
                    continue;
                start = double.Parse(row["VAAART"].ToString().Substring(2));
            }
            VSDDAT.SD22.RESETSEQ("0022", prefix, "VAART", start);
            return start;
        }
        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            IsReset = !IsReset;
            IsReset = !IsReset;
            if (KOLL.Trim() == "")
            {
                MessageBox.Show("Please choose a prefix");
                return;
            }
            if (dwMaster == null)
                return;
            prefix = KOLL.Substring(0, 2);
            string newart = "";
            newart = (prefix + VSDDAT.SD22.GETNEXT("0022", prefix, "VAART", 1, 6)).PadRight(24);
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["VAAARTST"] = dwMaster["VAAARTST"];
            dwData["VAAKOLL"] = KOLL;
            dwData["CODE"] = dwMaster["CODE"];
            dwData["BOOK"] = dwMaster["BOOK"];
            dwData["PRICE"] = dwMaster["PRICE"];
            dwData["LOCATION"] = dwMaster["LOCATION"];
            dwData["LSLIEFNA"] = dwMaster["LSLIEFNA"];
            dwData["VAAZOLLTNR1"] = dwMaster["CODE"].ToString().PadRight(20).Substring(0,20);
            dwData["VAAFILLER"] = dwMaster["VAAFILLER"];
            dwData["VAABEZ"] = dwMaster["VAABEZ"].ToString().Substring(0, 4).PadRight(30); ;
            dwData["VAAREFART"] = dwMaster["LOCATION"];
            dwData["VAAARTFAM"] = dwMaster["BOOK"];
            dwData["VAAARTWEB"] = dwMaster["PRICE"];
            dwData["VAAART"] = newart;
            dwData["VAADART"] = dwData["VAAART"].ToString().Trim().PadRight(20).Substring(0, 20);
            dwData["VAADVAR"] = "  ";
            dwData["VAAPART"] = dwData["VAADART"];
            dwData["VAANEUBEN"] = app.User;
            dwData["VAANEUDAT"] = MV.date;
            TMV.Insert(dwData);
            dwData = cART.TBMain.NewRow();
            dwData.ItemArray = cART.dwMaster.ItemArray;
            dwData["ARTICLE"] = newart;
            dwData["DESCRIPTION"] = dwMaster["VAABEZ"].ToString().Substring(0, 4).PadRight(30);
            cART.TMV.InserICC_EXART(dwData);
            dwData = cWS.TBMain.NewRow();
            dwData.ItemArray = cWS.dwMaster.ItemArray;
            dwData["WAART"] = newart.PadRight(20).Substring(0,20);
            dwData["WABEZ"] = dwMaster["VAABEZ"].ToString().Substring(0, 4).PadRight(30);
            cWS.TMV.InsertWASTAMM(dwData);
            MV.ART = "";
            vwMaster.View.MoveCurrentToFirst();
            IsEdit = true;

        }
        string prefix = "";
        public string GetArt()
        {
            IsReset = !IsReset;
            IsReset = !IsReset;
            prefix = KOLL.Substring(0, 2);
            string start = VSDDAT.SD22.GETNEXT("0022", prefix, "VAART", 1, 6);
            if (long.Parse(start) == 0)
            {
                MessageBox.Show("The serial number is wrong");
                return "0";
            }
            string art = (prefix + start).PadRight(24);
            if (long.Parse(start) == 999999)
            {
                dwData = TMV.VVAK.TBMain.NewRow();
                TMV.VVAK.SetUpNewRow(ref dwData);
                dwData["VAKMANDANT"] = app.Mandant;
                dwData["VAKKOLL"] = KOLL;
                dwData["VAKSPERRDAT"] = TMV.date;
                TMV.VVAK.UVAK02(dwData);
                dwData = TMV.VVAK.TBMain.NewRow();
                TMV.VVAK.SetUpNewRow(ref dwData);
                dwData["VAKMANDANT"] = app.Mandant;
                int idx = 0;
                foreach (string a in TMV.WA)
                {
                    if (a == KOLL.Substring(1, 1))
                        break;
                    idx++;
                }
                dwData["VAKKOLL"] = ("A" + TMV.WA[idx + 1]).PadRight(5);
                dwData["VAKSPERRDAT"] = 0;
                dwData["VAKKM1"] = app.company.PadRight(5);
                TMV.VVAK.InsertVAKOLL(dwData);
                MessageBox.Show("it is big event,please have a coffe and close this app");
            }
            IsReset = !IsReset;
            IsReset = !IsReset;
            return art;
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (!IsReady)
                {
                    app.Mandant = "99";
                    QueryCommand(null, null);
                    app.Mandant = "00";
                }
                IsPrint = false;
                IsReset = !IsReset;
                IsReset = !IsReset;
                if (KOLL.Trim() == "")
                {
                    MessageBox.Show("Please choose a prefix");
                    return;
                }
                if (!IsNew)
                {
                    prefix = KOLL.Substring(0, 2);
                    IsNew = true;
                    IsSelfART = true; 
                    NART =GetArt();
                    EXART = "AHK-";
                    PRODGRP = prefix.PadRight(3);
                    return;
                }
                if (!IsSelfART)
                {
                    NART = EXART;
                }
                else
                {
                    if (PRODGRP.Trim().Length == 0)
                    {
                        MessageBox.Show("请先输入产品组");
                        return;
                    }
                }
                if (NART.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入品种编号");
                    return;
                }
                if (EXART.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入供应商品种编号");
                    return;
                }
                if (ARTGRP.Trim().Length == 0)
                {
                    ARTGRP = "   ";
                }
                if (!IsSameProdArt)
                {
                    if (EXART.Trim().Length == 0)
                    {
                        MessageBox.Show("请先定义生产品种");
                        return;
                    }
                }
                if (NART.Contains("'"))
                    return;
                PM["ART"] = NART;
                PM["KOLL"] = KOLL;
                PM["ARTST"] = ARTST;
                PM["VAAPRGR"] = PRODGRP;
                PM["VAAARTGRP"] = ARTGRP;
                PM["EXART"] = EXART;
                PM["VAAZOLLTNR1"] = VAAZOLLTNR1;
                PM["VAAREFART"] = VAAREFART;
                PM["VAAARTFAM"] = VAAARTFAM;
                PM["DES"] = " ";
                TMV.NEWVAART(PM);
                vwMaster.View.MoveCurrentToFirst();
                //base.NewCommand(sender, e);
                IsKEY = true;
                IsEdit = true;
                IsSaved = false;
                IsNew = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
        }
        public override void OnReSetChg()
        {
            //base.OnReSetChg();
        }
        
       
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TMV.CALL = 0;
                TMV.CNOW = 0;
                IsBusy = true;
                IsReady = true;
                string k = "";
                dwData = TBMain.NewRow();
                dwData["VAAMANDANT"] = app.Mandant;
                dwData["VAAKOLL"] = "";
                dwData["VAAARTST"] = ((VVAART)MV).ARTST;
                dwData["VAAART"] = ((VVAART)MV).ART.Trim();
                dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
                dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
                dwData["VAAEXCL"] = IMAGEPATH;
                dwData["VAAFILLER"] = txtCOMP.Text.ToString().Trim();
                dwData["VAAREFART"] = txtLOCATION.Text.ToString().Trim();
                dwData["VAAARTFAM"] = txtBOOK.Text.ToString().Trim();
                if (SHIFT.Length == 0)
                {
                    dwData["VAAARTFIN"] = "";
                }
                else
                {
                    dwData["VAAARTFIN"] = " AND RTRIM(SUPPLIER) = '" + SHIFT.Trim() + "' ";
                }
                //供应商
                SResult = AFSPageLoad(dwData);

                MergeData(SResult, false);
                cLS02.vwMaster.View.MoveCurrentTo(-1);
                if (!TMV.IsPrint)
                {
                    TBDetail.Merge(TBMain);
                    TBDetail.Clear();
                    if (vwDetail != null)
                        vwDetail.Source = TBDetail;
                }
                base.QueryCommand(sender, e);
                IsBusy = false;
                SHIFT = "";

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                DES = dwMaster["VAABEZ"].ToString();
                cWS.SaveCommand(null, null);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WS2VA":
                    UpdateByWS(msg);
                    break;
            }
        }

        public   void ExportCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DataSet Set = new DataSet();
                Set.Merge(TBMain); 
                cDR.DocType = "AFSARTLIST";
                cDR.PrintType = "OpenExcel";
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void PrintCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                TB.TableName = TMV.TableName;
                if (TBDetail.Rows.Count > 0)
                {
                    TB.Merge(TBDetail);
                    foreach (DataRow dwWB in TBDetail.Rows)
                    {
                        dwData = TMV.VWS.TBMain.NewRow();
                        dwData["WAMANDANT"] = app.Mandant;
                        dwData["WAART"] = dwWB["VAADART"].ToString();
                        SResult = TMV.VWS.QWS13(dwData);
                        Set.Merge(SResult);
                    }
                }
                else
                {
                    foreach (var item in dlgMaster.SelectedItems)
                    {
                        if (!item.GetType().ToString().Contains("DataRowView"))
                            continue;

                        DataRow _dwWB = ((System.Data.DataRowView)item).Row;
                        DataRow dwWB = TB.Rows.Add();
                        dwWB.ItemArray = _dwWB.ItemArray;
                        dwData = TMV.VWS.TBMain.NewRow();
                        dwData["WAMANDANT"] = app.Mandant;
                        dwData["WAART"] = dwWB["VAADART"].ToString();
                        SResult = TMV.VWS.QWS13(dwData);
                        Set.Merge(SResult);
                    }
                }

                TB.TableName = "VAART";
                Set.Merge(TB);
                cDR.DocType = VALBTYP;
                cDR.PrintType = "DirectPrint";
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        
        public void ReactiveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                dwMaster["VAASPERRDAT"] = 0;
                TMV.UVAERRDAT(dwMaster);
                vwMaster.View.MoveCurrentToNext();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwMaster["VAASPERRDAT"] =TMV.date;
            TMV.UVAERRDAT(dwMaster);
            cEit.IsEnabled = false;
        }

        public void PrintExCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                TB.TableName = TMV.TableName;
                if (TBDetail.Rows.Count > 0)
                {
                    TB.Merge(TBDetail);
                    foreach (DataRow dwWB in TBDetail.Rows)
                    {
                        dwData = TMV.VWS.TBMain.NewRow();
                        dwData["WAMANDANT"] = app.Mandant;
                        dwData["WAART"] = dwWB["VAADART"].ToString();
                        SResult = TMV.VWS.QWS13(dwData);
                        Set.Merge(SResult);
                    }
                }
                else
                {
                    foreach (var item in dlgMaster.SelectedItems)
                    {
                        if (!item.GetType().ToString().Contains("DataRowView"))
                            continue;

                        DataRow _dwWB = ((System.Data.DataRowView)item).Row;
                        DataRow dwWB = TB.Rows.Add();
                        dwWB.ItemArray = _dwWB.ItemArray;
                        dwData = TMV.VWS.TBMain.NewRow();
                        dwData["WAMANDANT"] = app.Mandant;
                        dwData["WAART"] = dwWB["VAADART"].ToString();
                        SResult = TMV.VWS.QWS13(dwData);
                        Set.Merge(SResult);
                    }
                }
                TB.TableName = "VAART";
                Set.Merge(TB);
                cDR.PrintType = "OpenExcel";
                cDR.DocType = VALBTYP;
                cDR.PRINT(Set);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintVA06_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Clone();
            string sind = "", ART = "";
            Set.Merge(TMV.SetProd);
            cDRVA06.PRINT(Set);
        }
        public override void OnLIEFNRChg(string ls)
        {
            if (dwMaster == null)
                return;
            dwMaster["LSLIEFNA"] = LSLIEFNA;
            base.OnLIEFNRChg(ls);
        }
        private void ART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;

                this.Focus();
                TextBox tb = sender as TextBox;
                QueryCommand(sender, null);
                tb.Focus();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsImageMax = !IsImageMax;
        }
        private void cImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {

        }

        DataTable tbArt;
        private void ReimportCommand(object sender, RoutedEventArgs e)
        {
            if (KOLL.Trim() == "")
            {
                MessageBox.Show("Please choose the Prefix");
                return;
            }
            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = TMV.KOLL;
            TMV.DVAART01(dwData);
            dwData = TMV.VEXART.TBMain.NewRow();
            TMV.VEXART.SetUpNewRow(ref dwData);
            dwData["ARTICLE"] = "   ARTICLE LIKE '" + TMV.KOLL.PadRight(5).Substring(0, 2) + "%'";
            TMV.VEXART.DAM01(dwData);
            dwData = TMV.VWS.TBMain.NewRow();
            dwData["WAMANDANT"] = app.Mandant;
            dwData["WAART"] = " AND WAART LIKE '" + TMV.KOLL.PadRight(5).Substring(0, 2) + "%'";
            TMV.VWS.DWS010(dwData);
            TMV.VTXT.BATCHREMOVETEX(TMV.KOLL, "VKT");
        }
        private void Convert2LocalDB()
        {
            try
            {
                double start = RESET();
                int count = tbArt.Rows.Count;
                if (start + count > 999999)
                {
                    MessageBox.Show("The barcode number need to start from a new prefix. this message is create by Chinese SunLight on 2016/05/27 who is trying to make a happy APP.");
                    MessageBox.Show("To help SunLight, please import less records. Then manually create the last article to upgrage from AA->AB.");
                    return;
                }
                int idx = 0;
                foreach (DataRow row in tbArt.Rows)
                {
                    start++;
                    idx++;
                    EventDispatcher.Instance.RaiseStatus("On Processing " + idx.ToString() + "/" + count, StatusEventType.RunningProgress);
                    string art = prefix + Helper.Right("000000" + start.ToString().Trim(), 6);
                    dwData = TBMain.NewRow();
                    TMV.SetUpNewRow(ref dwData);
                    dwData["VAAKOLL"] = TMV.KOLL;
                    dwData["VAAARTST"] = "F";
                    dwData["VAAART"] = Helper.Left(art.PadRight(24), 24);
                    dwData["VAADART"] = Helper.Left(art.PadRight(20), 20);
                    dwData["VAABEZ"] = Helper.Left(row["ARTICLE"].ToString().Trim().PadRight(30), 30);
                    dwData["VAAFILLER"] = Helper.Left(row["COMPOSITION"].ToString().Trim().PadRight(72), 72);
                    //dwData["VAAGEWVON"] = double.Parse(row["WEIGHT"].ToString()) * 100;
                    //dwData["VAABRTBIS"] = double.Parse(row["WIDTH"].ToString()) * 100;  
                    dwData["VAAZOLLTNR1"] = Helper.Left(row["CODE"].ToString().Trim().PadRight(20), 20);
                    dwData["VAAREFART"] = Helper.Left(row["LOCATION"].ToString().Trim().PadRight(24), 24);
                    dwData["VAAARTFAM"] = Helper.Left(row["BOOK"].ToString().Trim().PadRight(24), 24);
                    dwData["VAAARTWEB"] = Helper.Left(row["CUSTOMER PRICE"].ToString().Trim().PadRight(24), 24);
                    dwData["VAAAENDBEN"] = app.User;
                    dwData["VAAAENDDAT"] = TMV.date;
                    TBMain.Rows.InsertAt(dwData, 0);
                    try
                    {
                        TMV.InsertVAART(dwData);
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                    dwData = TMV.VEXART.TBMain.NewRow();
                    TMV.VEXART.SetUpNewRow(ref dwData);
                    dwData["ARTICLE"] = Helper.Left(art.PadRight(24), 24);
                    dwData["DESCRIPTION"] = Helper.Left(row["ARTICLE"].ToString().Trim().PadRight(30), 30);
                    dwData["COMPOSITION"] = Helper.Left(row["COMPOSITION"].ToString().Trim().PadRight(72), 72);
                    dwData["WEIGHT"] = row["WEIGHT"].ToString();
                    dwData["WIDTH"] = row["WIDTH"].ToString();
                    dwData["CODE"] = Helper.Left(row["CODE"].ToString().Trim().PadRight(20), 20);
                    dwData["LOCATION"] = Helper.Left(row["LOCATION"].ToString().Trim().PadRight(24), 24);
                    dwData["BOOK"] = Helper.Left(row["BOOK"].ToString().Trim().PadRight(24), 24);
                    dwData["CUSTOMERPRICE"] = Helper.Left(row["CUSTOMER PRICE"].ToString().Trim().PadRight(24), 24);
                    string SUPPLIER = Helper.Left(row["SUPPLIER"].ToString().Trim().PadRight(8), 8);
                    dwData["SUPPLIER"] = SUPPLIER;
                    PM["LSLIEFNA"] = SUPPLIER;
                    string remark = row["REMARK"].ToString().Trim();
                    dwData["REMARK"] = remark.Replace("'", "''");
                    TMV.VEXART.InserICC_EXART(dwData);
                    //供应商
                    string lscode = TMV.VLS.CHKNEW(PM);

                    dwData = TMV.VWS.TBMain.NewRow();
                    TMV.VWS.SetUpNewRow(ref dwData);
                    dwData["WAMANDANT"] = app.Mandant;
                    dwData["WAFKZ"] = "J";
                    dwData["WAART"] = Helper.Left(art.PadRight(20), 20);
                    dwData["WAVAR"] = "  ";
                    dwData["WABEZ"] = Helper.Left(row["ARTICLE"].ToString().Trim().PadRight(40), 40);
                    dwData["WABIND"] = ("0.00" + Helper.Left(row["CUSTOMER PRICE"].ToString().Trim(), 8)).PadRight(12) + "M  ";
                    dwData["WALIEFNR"] = lscode;
                    TMV.VWS.Insert(dwData);
                    string sind = (TMV.KOLL.PadRight(5) + art).PadRight(60);
                    if (remark.Trim().Length == 0)
                        continue;
                    //备注
                    TMV.VTXT.BATCHBOXTEX(sind, "VKT", remark.Replace("'", "''"), true);
                    // TMV.BATCHWB14(dwData);
                }
                VSDDAT.SD22.RESETSEQ("0022", prefix, "VAART", start);
                EventDispatcher.Instance.RaiseStatus("Done" + count + ",You can only restart after many imports", StatusEventType.StopProgress);
                //TMV.POSTCHG();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            //MessageBox.Show("You can only quit after many imports");
            this.IsEnabled = false;
        }
        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Convert2LocalDB();
            base.bgWorker_DoWork(sender, e);
        }
        private void LoadExcelCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                IsReset = !IsReset;
                IsReset = !IsReset;
                this.IsBusy = true;
                if (bgWorker == null)
                    bgWorker = new BackgroundWorker();
                TBMain.Clear();
                cDREx.PRINT(null);
                if (cDREx.SetExchange == null)
                    return;
                if (cDREx.SetExchange.Tables.Count == 0)
                    return;
                foreach (DataRow row in cDREx.SetExchange.Tables[0].Rows)
                {
                    foreach (DataColumn c in cDREx.SetExchange.Tables[0].Columns)
                    {
                        row[c] = Helper.ConvertGB2312To8859P1(row[c].ToString());
                    }
                }
                tbArt = cDREx.SetExchange.Tables[0];
                //Convert2LocalDB();
                bgWorker.RunWorkerAsync();//
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void BarCode_KeyUp(object sender, KeyEventArgs e)
        {

            IsPrint = true;
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            this.Focus();
            TextBox tb = sender as TextBox;
            ART = tb.Text;
            QueryCommand(sender, e);
            if (TBDetail.Rows.Count == 0)
            {
                TBDetail.Merge(TBMain);
                TBDetail.Clear();
                if (vwDetail != null)
                    vwDetail.Source = TBDetail;
            }
            vwMaster.View.MoveCurrentToFirst();
            tb.Focus();
        }

        private void ResetAACommand(object sender, RoutedEventArgs e)
        {
            RESET();
        }
    }
}
